package com.sparrow.mvc.ui;

import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.sparrow.constant.CONFIG;
import com.sparrow.utils.Config;

@SuppressWarnings("serial")
public class JIFrame extends TagSupport {
	private String id;
	private String name;
	private String src;
	private String frameborder = "0";
	private String scrolling = "no";
	private String cssText = "";
	private String cssClass = "";

	public String getCssText() {
		if (!this.cssText.trim().equals("")) {
			this.cssText = String.format(" style=\"%1$s\" ", this.cssText);
		}
		return cssText;
	}

	public void setCssText(String cssText) {
		this.cssText = cssText;
	}

	public String getFrameborder() {
		return String.format(" frameborder=\"%1$s\" ", this.frameborder);
	}

	public void setFrameborder(String frameborder) {
		this.frameborder = frameborder;
	}

	public String getScrolling() {
		return String.format(" scrolling=\"%1$s\" ", this.scrolling);
	}

	public void setScrolling(String scrolling) {
		this.scrolling = scrolling;
	}

	public String getSrc() {
		if (this.src != null) {
			this.src = String.format(" src=\"%1$s\" ", this.src.replace(
					"$rootPath", Config
							.getValueInCache(CONFIG.ROOT_PATH)));
		}

		return src;
	}

	public void setSrc(String src) {
		this.src = src;
	}

	@Override
	public int doStartTag() throws JspException {
		int returnValue = TagSupport.SKIP_BODY;
		StringBuffer writeHTML = new StringBuffer("<iframe");
		writeHTML.append(this.getId());
		writeHTML.append(this.getName());
		writeHTML.append(this.getCssClass());
		writeHTML.append(this.getCssText());
		writeHTML.append(this.getFrameborder());
		writeHTML.append(this.getScrolling());
		writeHTML.append(this.getSrc());
		writeHTML.append(">");
		try {
			this.pageContext.getOut().print(writeHTML + "</iframe>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return returnValue;
	}

	public String getCssClass() {
		if (!this.cssClass.trim().equals("")) {
			this.cssClass = String.format(" class=\"%1$s\"", this.cssClass);
		}
		return this.cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	@Override
	public String getId() {
		if (!this.id.trim().equals("")) {
			this.id = String.format(" id=\"%1$s\"", this.id);
		}
		return this.id;
	}

	@Override
	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		if (this.name != null && !this.name.trim().equals("")) {
			return String.format(" name=\"%1$s\"", this.name);
		} else {
			return "";
		}
	}

	public void setName(String name) {
		this.name = name;
	}
}
